import Vue from "vue"
import axios from "axios";

// 初始化axios的通用配置
const service=axios.create({
  baseURL: process.env.VUE_APP_BASE_API
})
// request请求统一拦截器
service.interceptors.request.use(config=>{

  // config.headers['Content-Type']='application/x-www.form-urlencoded;charset=utf-8'
  const  token =Vue.$cookies.get('token')
  if (token){
    config.headers.Authorization=token
  }

  return config
})


const http={
  get(url,params){
    return service.get(formatterUrl(url,params))
  },
  post(url,params){
    return service.post(formatterUrl(url,params))
  }
}

function formatterUrl(url, params) {
  let formatterParams=''
  if (params){
    if (typeof params==='object'){
      if (Object.keys(params).length>0){
        for (const p in params) {
          formatterParams+=`${p}=${params[p]}&`
        }
        formatterParams=formatterParams.substr(0,formatterParams.length-1)
        url=`${url}?${formatterParams}`
      }
    }

  }
  return url
}
Vue.prototype.$axios=service
Vue.prototype.$http=http
Vue.prototype.$ajax=axios
